<template>
  <div class="applyPage">
    <el-row>
      <el-col style="text-align: center;">
        <el-input
          v-model.trim="username"
          placeholder="输入单位名称"
          :style="'width:50%;margin:5px 0px;'"
          @keyup.enter.native="search"
        >
          <el-button slot="append" @click="search">搜索</el-button>
        </el-input>
      </el-col>
    </el-row>
    <el-row>
      <el-col :sm="4">
        <div class="g-title">用车审核({{total}}个）</div>
      </el-col>
    </el-row>
    <el-table
      ref="multipleTable"
      :data="listData"
      tooltip-effect="dark"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column type="index" label="序号" width="55" align="center"></el-table-column>
      <el-table-column prop="companyName" label="申请单位" show-overflow-tooltip align="center"></el-table-column>
      <el-table-column prop="userName" label="申请人" align="center"></el-table-column>
      <el-table-column prop="processType" label="联系电话" align="center"></el-table-column>
      <el-table-column prop="createtime" label="申请时间" align="center"></el-table-column>
      <el-table-column prop="createtime" label="始发地" align="center"></el-table-column>
      <el-table-column prop="createtime" label="目的地" align="center"></el-table-column>
      <el-table-column prop="createtime" label="操作" align="center">
        <template slot-scope="scope">
          <div>
            <el-button @click="viewDetail(scope.row)" type="primary" size="small">查看</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-row style="padding: 32px 16px;">
      <el-col>
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :page-size="pageSize"
          :total="total"
          :current-page="pageNo"
          @current-change="fetchData"
          @size-change="pageSizeChange"
        ></el-pagination>
      </el-col>
    </el-row>
    <userCarApply :dialog-visible.sync="userCarApplyVisible" @finish="userCarApplyFinish"></userCarApply>
    <useCarView :dialog-visible.sync="dialogVisible" :data="formData" :history="history"></useCarView>
  </div>
</template>
<script>
import "./index.scss";
import { applyListjson } from "@/api/usecar";
import { viewJson } from "@/api/task";
import userCarApply from "./components/userCarApply/index";
import useCarView from "./components/useCarView/index";
export default {
  components: { userCarApply, useCarView },
  data: function() {
    return {
      username: "",
      dialogVisible: false,
      roleDialogVisible: false,
      total: 0,
      pageNo: 1,
      pageSize: 10,
      listData: [], //全部数据
      listDataSelected: [], //选中数据
      formData: null,
      userCarApplyVisible: false, //申请用车组件可见
      history: [] //历史流程
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    //申请用车完成回调
    userCarApplyFinish(data) {
      if (data && data.error == 0) {
        this.userCarApplyVisible = false;
        this.$message.success("申请已提交");
        this.pageNo = 1;
        this.fetchData();
      } else {
        this.$message.error("操作失败，请重试");
      }
    },
    //查看详情
    async viewDetail(data) {
      console.log(data);
      let r = await viewJson({
        tableId: data.id,
        pid: data.prcid,
        formKey: "ddusecar"
      });
      let { history, usecar } = r;
      this.formData = usecar;
      this.history = history;
      this.history.splice(0, 0);
      this.dialogVisible = true;
    },
    //搜索
    search() {
      this.pageNo = 1;
      this.fetchData();
    },
    //导出
    exportData() {
      if (this.listDataSelected.length == 0) {
        this.$message("请选择");
        return;
      }
      var headers =
        "姓名,内设机构,职务,手机号,办公电话,角色,登录名,是否司机,注册日期,状态,密码";
      var data = this.listDataSelected.map(function(e) {
        return {
          username: e.username || "",
          deptName: e.deptName || "",
          zw: e.zw || "",
          mobile: e.mobile || "",
          phone: e.phone || "",
          roleNames: e.roleNames.replace(/,/g, "，"),
          loginname: e.loginname,
          dr: e.dr == 1 ? "是" : "否",
          registtime: e.registtime,
          zt: e.zt || "",
          pwd: e.pwd
        };
      });
      this.jsonToExcel(data, headers);
    },
    //json导出excel
    jsonToExcel(jsonData, headers) {
      //列标题，逗号隔开，每一个逗号就是隔开一个单元格
      var str = headers + "\n";
      //增加\t为了不让表格显示科学计数法或者其他格式
      for (var i = 0; i < jsonData.length; i++) {
        for (var item in jsonData[i]) {
          str += jsonData[i][item] + "\t,";
        }
        str += "\n";
      }
      //encodeURIComponent解决中文乱码
      var uri = "data:text/csv;charset=utf-8,\ufeff" + encodeURIComponent(str);
      //通过创建a标签实现
      var link = document.createElement("a");
      link.href = uri;
      //对下载的文件命名
      link.download = "导出数据表.csv";
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    },
    pageSizeChange(newSize) {
      this.pageSize = newSize;
      this.pageNo = 1;
      this.fetchData();
    },
    async fetchData(pageNo) {
      if (pageNo) {
        this.pageNo = pageNo;
      }
      let params = {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        username: this.username
      };
      let data = await applyListjson(params);
      this.listData = data.data;
      this.total = data.total;
      this.listDataSelected.splice(0);
    },
    //表格多选
    handleSelectionChange(val) {
      this.listDataSelected.splice(0);
      val.forEach(function(e) {
        app.listDataSelected.push(e);
      });
      console.log(this.listDataSelected);
    }
  }
};
</script>